//
//  BiLoginNetEngine.m
//  TrunkIndustryManageSystem
//
//  Created by fanbo on 15/4/17.
//  Copyright (c) 2015年 李璐西. All rights reserved.
//

#import "BiLoginNetEngine.h"
#import "BiNetEngine.h"
#import "ASINetworkQueue.h"
#import "ASIFormDataRequest.h"
#import "BiLoginParser.h"

@implementation BiLoginNetEngine
SINGLE_INSTANCE_IMPLEMENTION(BiLoginNetEngine)


-(void)requestLogin:(id) delegte  userName:(NSString *)username passWord:(NSString *) pwd
{
    
//    NSString *url = [NSString stringWithFormat:@"%@/erp/mobile.php/CarXingxiao/saveXingxiao", SERVICE_URL];
    NSString *url = [NSString stringWithFormat:@"%@/erp/mobile.php/Login", SERVICE_URL];
    NSDictionary * paramDic = [NSDictionary dictionaryWithObjectsAndKeys:username, @"username", pwd, @"password",nil];
    [_dataDic setObject:delegte forKey:[NSNumber numberWithInt:NET_LOG]];
    
    [self createPostRequest:url paramDic:paramDic tag:NET_LOG];
}

- (void)requestDone:(ASIHTTPRequest *)request
{
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    int httpCode = [request responseStatusCode];
    
    if (200 > httpCode || 300 <= httpCode)
    {
        NSLog(@"http error --- tag = %d, http code = %d", tag, httpCode);
        assert(0);
        return;
    }
    
    switch (tag)
    {
        case NET_LOG:
        {
            [self onGetCarSaleCheckListData:request];
        }
            break;
        default:
            break;
    }
    
}

- (void)onGetCarSaleCheckListData:(ASIHTTPRequest *)request
{
    int httpCode = [request responseStatusCode];
    
    if (200 != httpCode)
    {
        return;
    }
    
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    id delegate = [_dataDic objectForKey:[NSNumber numberWithInt:tag]];
    [_dataDic removeObjectForKey:[NSNumber numberWithInt:tag]];  // 请求完后清除掉delegate
    
    if (nil != delegate && [delegate respondsToSelector:@selector(onNetEngineResponse:entity:userInfo:)])
    {
        id  entity  = [BiLoginParser parserUserEntity:[request responseString]];
        [delegate onNetEngineResponse:tag entity:entity userInfo:nil];
    }
}


@end
